package com.csglxt.service.impl;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csglxt.base.DaoSupportImpl;
import com.csglxt.domain.Goods;
import com.csglxt.domain.OrderItem;
import com.csglxt.domain.Storage;
import com.csglxt.service.StorageService;

/**
 * 
 * @ClassName: StorageServiceImpl
 * @Description: TODO(库存业务实现类 )
 * @author Itchbeilef
 * @date 2015-5-28 下午10:17:01
 * 
 */

@Service
@Transactional
public class StorageServiceImpl extends DaoSupportImpl<Storage> implements
        StorageService {

    /**
     * 
     * @Title: queryStorageByGoods
     * @Description: TODO(根据 商品来查询库存 )
     * @param Goods
     *            商品
     * @return
     * @throws
     */
    @SuppressWarnings("unchecked")
    public List<Storage> queryStorageByGoods(Goods goods) {
        return getSession().createQuery("FROM Storage s WHERE s.goods = ?")//
                .setParameter(0, goods)//
                .list();
    }

    /**
     * 
     * @Title: UpGoodsNumbyOrderItem
     * @Description: TODO(跟据单据明细信息增加或者减少相对应商品在库存表里的数量)
     * @param item
     *            ：单据明细项 jia：增加或减少
     * @return
     * @throws
     */
    public int UpGoodsNumbyOrderItem(OrderItem item, boolean jia) {
        int result = 0;
        if (jia) {
            result = getSession()
                    .createQuery(
                            "UPDATE Storage s SET s.goodsNumber=s.goodsNumber+? WHERE s.goods.id=?")
                    .setParameter(0, item.getNumber())
                    .setParameter(1, item.getGoods().getId()).executeUpdate();
        } else {
            result = getSession()
                    .createQuery(
                            "UPDATE Storage s SET s.goodsNumber=s.goodsNumber-? WHERE s.goods.id=?")
                    .setParameter(0, item.getNumber())
                    .setParameter(1, item.getGoods().getId()).executeUpdate();
        }
        return result;
    }

    /**
     * 
     * @Title: UpGoodsNumbyOrderItems
     * @Description: TODO((跟据单据明细集合信息增加或减少库存表对应商品数量)
     * @param items
     *            ：单据明细项信息集合 jia：增加或减少
     * @return
     * @throws
     */
    public int UpGoodsNumbyOrderItems(Set<OrderItem> items, boolean jia) {
        int result = 0;
        for (OrderItem item : items) {
            result = UpGoodsNumbyOrderItem(item, jia);
        }
        return result;
    }

    /**
     * 
     * @Title: queryGoodsNumbygoodsid
     * @Description: TODO(根据 商品ID来查询商品 库存数)
     * @param Goodsid
     *            商品ID marketid 超市ID
     * @return
     * @throws
     */
    public int queryGoodsNumbygoodsid(String Goodsid) {
        Number result = (Number) getSession()
                .createQuery(
                        "SELECT s.goodsNumber From Storage s WHERE s.goods.id=?")
                .setParameter(0, Goodsid).uniqueResult();
        return result.intValue();
    }

}
